package com.lj.luoye.lotteryticket.analysis;

import com.lj.luoye.lotteryticket.entity.Unionlotto;
import com.lj.luoye.lotteryticket.entity.UnionlottoStatistic;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

public abstract class UnionlottoAnalyzer {
    private final int maxRedCount=8;
    private final int maxBlueCount=2;

    protected List<Unionlotto> unionlottoList;
    //protected UnionlottoStatistic unionlottoStatistic;

    public UnionlottoAnalyzer() {
        this.unionlottoList=new ArrayList<Unionlotto>();
        //this.unionlottoStatistic=new UnionlottoStatistic();
    }

    public abstract void analyze(AnalyzeCallback analyzeCallback,int count,boolean multi);
    public abstract void analyze(AnalyzeCallback analyzeCallback,int count);
    public abstract void analyzeMulti(AnalyzeCallback analyzeCallback,int count,int redCount,int blueCount);

    public UnionlottoStatistic doStatistic(List<Unionlotto> unionlottoListParam){
        UnionlottoStatistic unionlottoStatistic=new UnionlottoStatistic();
        //统计
        for(Unionlotto unionlotto:unionlottoListParam){
            for(String redBall:unionlotto.getRedBalls()){
                Integer redCount=unionlottoStatistic.getRedBallCount().get(redBall);
                if(redCount==null){
                    unionlottoStatistic.getRedBallCount().put(redBall,1);
                }else{
                    redCount=redCount.intValue()+1;
                    unionlottoStatistic.getRedBallCount().put(redBall,redCount);
                }
            }
            Integer blueCount=unionlottoStatistic.getBlueBallCount().get(unionlotto.getBlueBall());
            if(blueCount==null){
                blueCount=0;
            }
            blueCount=blueCount.intValue()+1;
            unionlottoStatistic.getBlueBallCount().put(unionlotto.getBlueBall(),blueCount);
        }
        System.out.println(unionlottoStatistic);
        System.out.println(unionlottoStatistic.getRedBallCount());
        System.out.println(unionlottoStatistic.getBlueBallCount());
        //补齐数据
        for(int i=1;i<=33;i++){
            String r=String.format("%02d",i);
            Integer rc=unionlottoStatistic.getRedBallCount().get(r);
            if(rc==null){
                unionlottoStatistic.getRedBallCount().put(r,0);
            }
        }
        for(int i=1;i<=16;i++){
            String b=String.format("%02d",i);
            Integer bc=unionlottoStatistic.getBlueBallCount().get(b);
            if(bc==null){
                unionlottoStatistic.getBlueBallCount().put(b,0);
            }
        }
        System.out.println(unionlottoStatistic.getRedBallCount());
        System.out.println(unionlottoStatistic.getBlueBallCount());
        return unionlottoStatistic;
    }

    public List<Unionlotto> doReduce(UnionlottoStatistic unionlottoStatistic){
        //红球按数量从小到大排序
        Set<String> redBallKeys=unionlottoStatistic.getRedBallCount().keySet();
        String []redBallKeya=redBallKeys.toArray(new String[0]);
        int size=redBallKeya.length;
        for(int i=0;i<size;i++){
            for(int j=0;j<size-i-1;j++){
                int count1=unionlottoStatistic.getRedBallCount().get(redBallKeya[j]);
                int count2=unionlottoStatistic.getRedBallCount().get(redBallKeya[j+1]);
                if(count1>count2){//交换
                    String temp=redBallKeya[j];
                    redBallKeya[j]=redBallKeya[j+1];
                    redBallKeya[j+1]=temp;
                }
            }
        }
        Map<String,Integer> redBallCount=new LinkedHashMap<String, Integer>();
        for (String s : redBallKeya) {
            redBallCount.put(s,unionlottoStatistic.getRedBallCount().get(s));
        }
        //System.out.println(redBallCount);
        unionlottoStatistic.setRedBallCount(redBallCount);
        System.out.println(unionlottoStatistic.getRedBallCount());
        //红球概率最高的一组和概率最低的一组
        String maxRedBalls[]=new String[6];
        for(int i=0;i<6;i++){
            maxRedBalls[i]=redBallKeya[size-i-1];
        }
        String minRedBalls[]=new String[6];
        for(int i=0;i<6;i++){
            minRedBalls[i]=redBallKeya[i];
        }


        //蓝球概率最高的一组和概率最低的一组
        String maxBlueBall="01";
        String minBlueBall="01";
        Set<String> blueBallKeys=unionlottoStatistic.getBlueBallCount().keySet();
        for(String blueBall:blueBallKeys) {
            if("01".equals(blueBall)){
                continue;
            }
            int count = unionlottoStatistic.getBlueBallCount().get(blueBall);
            int count1=unionlottoStatistic.getBlueBallCount().get(maxBlueBall);
            if(count>count1){
                maxBlueBall=blueBall;
            }
            int count2=unionlottoStatistic.getBlueBallCount().get(minBlueBall);
            if(count<count2){
                minBlueBall=blueBall;
            }
        }

        System.out.println("");
        System.out.println("");
        System.out.print("最大一组：");
        for(String redBall:maxRedBalls){
            System.out.print(redBall+" ");
        }
        System.out.println("- "+maxBlueBall);
        System.out.print("最小一组：");
        for(String redBall:minRedBalls){
            System.out.print(redBall+" ");
        }
        System.out.println("- "+minBlueBall);

        List<Unionlotto> result=new ArrayList<Unionlotto>();
        result.add(new Unionlotto(0,"","最大一组",Arrays.asList(maxRedBalls),maxBlueBall));
        result.add(new Unionlotto(0,"","最小一组",Arrays.asList(minRedBalls),minBlueBall));
        return result;
    }

    public List<Unionlotto> doReduceMulti(UnionlottoStatistic unionlottoStatistic,int redCount,int blueCount){
        if(redCount<6){
            redCount=6;
        }
        if(redCount>maxRedCount){
            redCount=maxRedCount;
        }
        if(blueCount<1){
            blueCount=1;
        }
        if(blueCount>maxBlueCount){
            blueCount=maxBlueCount;
        }


        //红球按数量从小到大排序
        Set<String> redBallKeys=unionlottoStatistic.getRedBallCount().keySet();
        String []redBallKeya=redBallKeys.toArray(new String[0]);
        int size=redBallKeya.length;
        for(int i=0;i<size;i++){
            for(int j=0;j<size-i-1;j++){
                int count1=unionlottoStatistic.getRedBallCount().get(redBallKeya[j]);
                int count2=unionlottoStatistic.getRedBallCount().get(redBallKeya[j+1]);
                if(count1>count2){//交换
                    String temp=redBallKeya[j];
                    redBallKeya[j]=redBallKeya[j+1];
                    redBallKeya[j+1]=temp;
                }
            }
        }
        Map<String,Integer> redBallCount=new LinkedHashMap<String, Integer>();
        for (String s : redBallKeya) {
            redBallCount.put(s,unionlottoStatistic.getRedBallCount().get(s));
        }
        //System.out.println(redBallCount);
        unionlottoStatistic.setRedBallCount(redBallCount);
        System.out.println(unionlottoStatistic.getRedBallCount());
        //红球概率最高的一组和概率最低的一组
        String maxRedBalls[]=new String[redCount];
        for(int i=0;i<redCount;i++){
            maxRedBalls[i]=redBallKeya[size-i-1];
        }
        String minRedBalls[]=new String[redCount];
        for(int i=0;i<redCount;i++){
            minRedBalls[i]=redBallKeya[i];
        }

        //蓝球按数量从小到大排序
        Set<String> blueBallKeys=unionlottoStatistic.getBlueBallCount().keySet();
        String []blueBallKeya=blueBallKeys.toArray(new String[0]);
        size=blueBallKeya.length;
        for(int i=0;i<size;i++){
            for(int j=0;j<size-i-1;j++){
                int count1=unionlottoStatistic.getBlueBallCount().get(blueBallKeya[j]);
                int count2=unionlottoStatistic.getBlueBallCount().get(blueBallKeya[j+1]);
                if(count1>count2){//交换
                    String temp=blueBallKeya[j];
                    blueBallKeya[j]=blueBallKeya[j+1];
                    blueBallKeya[j+1]=temp;
                }
            }
        }
        Map<String,Integer> blueBallCount=new LinkedHashMap<String, Integer>();
        for (String s : blueBallKeya) {
            blueBallCount.put(s,unionlottoStatistic.getBlueBallCount().get(s));
        }
        //System.out.println(blueBallCount);
        unionlottoStatistic.setBlueBallCount(blueBallCount);
        System.out.println(unionlottoStatistic.getBlueBallCount());
        //红球概率最高的一组和概率最低的一组
        String maxBlueBalls[]=new String[blueCount];
        for(int i=0;i<blueCount;i++){
            maxBlueBalls[i]=blueBallKeya[size-i-1];
        }
        String minBlueBalls[]=new String[blueCount];
        for(int i=0;i<blueCount;i++){
            minBlueBalls[i]=blueBallKeya[i];
        }

        System.out.println("");
        System.out.println("");
        System.out.print("最大一组：");
        for(String redBall:maxBlueBalls){
            System.out.print(redBall+" ");
        }
        System.out.println("- ");
        for(String blueBall:maxBlueBalls){
            System.out.print(blueBall+" ");
        }
        System.out.print("最小一组：");
        for(String redBall:minRedBalls){
            System.out.print(redBall+" ");
        }
        System.out.println("- ");
        for(String blueBall:minBlueBalls){
            System.out.print(blueBall+" ");
        }

        List<Unionlotto> result=new ArrayList<Unionlotto>();
        result.add(new Unionlotto(0,"","最大一组",Arrays.asList(maxRedBalls),Arrays.asList(maxBlueBalls)));
        result.add(new Unionlotto(0,"","最小一组",Arrays.asList(minRedBalls),Arrays.asList(minBlueBalls)));
        return result;
    }

    public void printResult(List<Unionlotto> result){
        System.out.println("结果：");
        //输出结果
        for(Unionlotto unionlotto:result){
            System.out.print(unionlotto.getDate()+":");
            for(String redBall:unionlotto.getRedBalls()){
                System.out.print(redBall+" ");
            }
            System.out.println("- "+unionlotto.getBlueBall());
        }
    }

    //全量最大最小值方法
    public List<Unionlotto> fullMaxAndMin(){
        List<Unionlotto> result=this.doReduce(this.doStatistic(this.unionlottoList));
        //printResult(result);
        return result;
    }

    public List<Unionlotto> fullMaxAndMinMulti(int redCount,int blueCount){
        List<Unionlotto> result=this.doReduceMulti(this.doStatistic(this.unionlottoList),redCount,blueCount);
        //printResult(result);
        return result;
    }

    //随机取组最大最小值方法
    public List<Unionlotto> randMaxAndMin(){
        int rand= (int) (Math.random()*10+1);
        System.out.println("随机数："+rand);

        List<Unionlotto> unionlottos=new ArrayList<Unionlotto>();
        int size=this.unionlottoList.size();
        for(int i=0;i<size;i++){
            if(i%rand!=0){
                unionlottos.add(this.unionlottoList.get(i));
            }
        }

        List<Unionlotto> result=this.doReduce(this.doStatistic(unionlottos));
        if(result!=null&result.size()>0){
            result.get(0).setDescription("随机最大");
        }
        if(result!=null&result.size()>1){
            result.get(1).setDescription("随机最小");
        }
        //printResult(result);
        return result;
    }

    //挨个求和法
    public List<Unionlotto> sumOneByOne(){
        List<Unionlotto> result=new ArrayList<Unionlotto>();

        int redBallSums[]=new int[6];
        int blueBallSum=0;
        for(Unionlotto unionlotto:this.unionlottoList){
            int blueBall=Integer.parseInt(unionlotto.getBlueBall());
            blueBallSum+=blueBall;
            for(int i=0;i<6;i++){
                redBallSums[i]+=Integer.parseInt(unionlotto.getRedBalls().get(i));
            }
        }
        //求余
        int rand= (int) (Math.random()*16+1);
        int blueBall=(blueBallSum+rand)%16+1;
        System.out.println(rand+":b:"+blueBall);
        Set<Integer> redBalls=new HashSet<Integer>();
        for(int i=0;redBalls.size()<6;i++){
            rand= (int) (Math.random()*33+1);
            int redBall=(redBallSums[i%6]+rand)%33+1;
            System.out.println(rand+":r:"+redBall);
            redBalls.add(redBall);
        }

        List<String> redBallList=new ArrayList<String>();
        for(int redBall:redBalls){
            redBallList.add(String.format("%02d",redBall));
        }
        Unionlotto unionlotto=new Unionlotto(0,"","求和随机",redBallList,String.format("%02d",blueBall));
        result.add(unionlotto);

        return result;
    }

    public List<Unionlotto> sumOneByOneReal(){
        List<Unionlotto> result=new ArrayList<Unionlotto>();

        int redBallSums[]=new int[6];
        int blueBallSum=0;
        for(Unionlotto unionlotto:this.unionlottoList){
            int blueBall=Integer.parseInt(unionlotto.getBlueBall());
            blueBallSum+=blueBall;
            for(int i=0;i<6;i++){
                redBallSums[i]+=Integer.parseInt(unionlotto.getRedBalls().get(i));
            }
        }
        //求余
        //int rand= (int) (Math.random()*16+1);
        int blueBall=(blueBallSum)%16+1;
        System.out.println("b:"+blueBall);
        Set<Integer> redBalls=new HashSet<Integer>();
        for(int i=0;redBalls.size()<6;i++){
            //rand= (int) (Math.random()*33+1);
            int redBall=(redBallSums[i%6]+(i/6))%33+1;
            System.out.println("r:"+redBall);
            redBalls.add(redBall);
        }

        List<String> redBallList=new ArrayList<String>();
        for(int redBall:redBalls){
            redBallList.add(String.format("%02d",redBall));
        }
        Unionlotto unionlotto=new Unionlotto(0,"","挨个求和",redBallList,String.format("%02d",blueBall));
        result.add(unionlotto);

        return result;
    }

    //随机算法
    public List<Unionlotto> randOne(){
        List<Unionlotto> result=new ArrayList<Unionlotto>();

        int blueBall=(int) (Math.random()*16+1);
        System.out.println("b:"+blueBall);
        Set<Integer> redBalls=new HashSet<Integer>();
        for(int i=0;redBalls.size()<6;i++){
            int redBall=(int) (Math.random()*33+1);
            System.out.println(":r:"+redBall);
            redBalls.add(redBall);
        }

        List<String> redBallList=new ArrayList<String>();
        for(int redBall:redBalls){
            redBallList.add(String.format("%02d",redBall));
        }
        Unionlotto unionlotto=new Unionlotto(0,"","随机一组",redBallList,String.format("%02d",blueBall));
        result.add(unionlotto);

        return result;
    }

    //补缺算法
    public List<Unionlotto> fillVacancy(List<Unionlotto> baseList){
        List<Unionlotto> result=new ArrayList<Unionlotto>();
        Set<String> redBalls=new HashSet<>();
        Set<String> blueBalls=new HashSet<>();
        for(Unionlotto unionlotto:baseList){
            for(String redBall:unionlotto.redBalls){
                redBalls.add(redBall);
            }
            blueBalls.add(unionlotto.blueBall);
        }
        List<String> remainRedBalls=new ArrayList<>();
        for(int i=1;i<=33;i++){
            String ball=String.format("%02d",i);
            if(!redBalls.contains(ball)){
                remainRedBalls.add(ball);
            }
        }
        List<String> remainBlueBalls=new ArrayList<>();
        for(int i=1;i<=16;i++){
            String ball=String.format("%02d",i);
            if(!blueBalls.contains(ball)){
                remainBlueBalls.add(ball);
            }
        }
        System.out.println("remainRedBalls->"+remainRedBalls);
        System.out.println("remainBlueBalls->"+remainBlueBalls);

        Set<String> selectRedBalls=new HashSet<>();
        int rrbs=remainRedBalls.size();
        if(rrbs<=6){
            for(String redBall:remainRedBalls){
                selectRedBalls.add(redBall);
            }
            for(int i=0;selectRedBalls.size()<6;i++){
                int redBall=(int) (Math.random()*33+1);
                System.out.println(":r:"+redBall);
                String redBallStr=String.format("%02d",redBall);
                selectRedBalls.add(redBallStr);
            }
        }else {
            for (int i = 0; selectRedBalls.size() < 6; i++) {
                int redBallIndex = (int) (Math.random() * rrbs);
                String redBall = remainRedBalls.get(redBallIndex);
                System.out.println("fillVacancy:r:" + redBall);
                selectRedBalls.add(redBall);
            }
        }
        String selectBlueBall="";
        int rbbs=remainBlueBalls.size();
        if(rbbs<=0){
            int blueBall=(int) (Math.random()*16+1);
            selectBlueBall=String.format("%02d",blueBall);
        }else {
            int bbi = (int) (Math.random() * rbbs);
            selectBlueBall = remainBlueBalls.get(bbi);
        }
        List<String> redBallList=new ArrayList<String>();
        for(String redBall:selectRedBalls){
            redBallList.add(redBall);
        }
        Unionlotto unionlotto=new Unionlotto(0,"","补缺一组",redBallList,selectBlueBall);
        result.add(unionlotto);

        return result;
    }

    //均匀算法
    public List<Unionlotto> average(int count){
        //红球计算
        List<String> redBalls=new ArrayList<>();
        for(int n=0;n<count;n+=5){
            List<String> redBallsAll=new ArrayList<>();
            for(int i=1;i<=33;i++){
                redBallsAll.add(String.format("%02d",i));
            }
            Collections.shuffle(redBallsAll);
            for(int i=0;i<30;i++){
                redBalls.add(redBallsAll.get(i));
            }
        }

        //蓝球计算
        int countCalculate =count;
        if(count%16!=0) {
            countCalculate = (count / 16 + 1) * 16;
        }
        List<String> blueBalls=new ArrayList<>();
        for(int i=0;i<countCalculate;i++){
            int blueBall=i%16+1;
            String ball=String.format("%02d",blueBall);
            blueBalls.add(ball);
        }
        Collections.shuffle(blueBalls);

        //组合为count组选号
        List<Unionlotto> result=new ArrayList<Unionlotto>();
        for(int i=0;i<count;i++){
            List<String> redBallList=new ArrayList<String>();
            for(int j=i*6;j<(i+1)*6;j++){
                redBallList.add(redBalls.get(j));
            }
            Unionlotto unionlotto=new Unionlotto(0,"","均布一组",redBallList,blueBalls.get(i));
            result.add(unionlotto);
        }
        return result;
    }


    //补缺算法
    public List<Unionlotto> fillVacancyMulti(List<Unionlotto> baseList,int redCount,int blueCount){
        if(redCount<6){
            redCount=6;
        }
        if(redCount>maxRedCount){
            redCount=maxRedCount;
        }
        if(blueCount<1){
            blueCount=1;
        }
        if(blueCount>maxBlueCount){
            blueCount=maxBlueCount;
        }

        List<Unionlotto> result=new ArrayList<Unionlotto>();
        Set<String> redBalls=new HashSet<>();
        Set<String> blueBalls=new HashSet<>();
        for(Unionlotto unionlotto:baseList){
            for(String redBall:unionlotto.redBalls){
                redBalls.add(redBall);
            }
            blueBalls.add(unionlotto.blueBall);
        }
        List<String> remainRedBalls=new ArrayList<>();
        for(int i=1;i<=33;i++){
            String ball=String.format("%02d",i);
            if(!redBalls.contains(ball)){
                remainRedBalls.add(ball);
            }
        }
        List<String> remainBlueBalls=new ArrayList<>();
        for(int i=1;i<=16;i++){
            String ball=String.format("%02d",i);
            if(!blueBalls.contains(ball)){
                remainBlueBalls.add(ball);
            }
        }
        System.out.println("remainRedBalls->"+remainRedBalls);
        System.out.println("remainBlueBalls->"+remainBlueBalls);
        //选出红球
        Set<String> selectRedBalls=new HashSet<>();
        int rrbs=remainRedBalls.size();
        if(rrbs<=redCount){
            for(String redBall:remainRedBalls){
                selectRedBalls.add(redBall);
            }
            for(int i=0;selectRedBalls.size()<redCount;i++){
                int redBall=(int) (Math.random()*33+1);
                System.out.println(":r:"+redBall);
                String redBallStr=String.format("%02d",redBall);
                selectRedBalls.add(redBallStr);
            }
        }else {
            for (int i = 0; selectRedBalls.size() < redCount; i++) {
                int redBallIndex = (int) (Math.random() * rrbs);
                String redBall = remainRedBalls.get(redBallIndex);
                System.out.println("fillVacancy:r:" + redBall);
                selectRedBalls.add(redBall);
            }
        }

        //选出蓝球
        Set<String> selectBlueBalls=new HashSet<>();
        int rbbs=remainBlueBalls.size();
        if(rbbs<=blueCount){
            for(String blueBall:remainBlueBalls){
                selectBlueBalls.add(blueBall);
            }
            for(int i=0;selectBlueBalls.size()<blueCount;i++){
                int blueBall=(int) (Math.random()*16+1);
                System.out.println(":r:"+blueBall);
                String blueBallStr=String.format("%02d",blueBall);
                selectBlueBalls.add(blueBallStr);
            }
        }else {
            for (int i = 0; selectBlueBalls.size() < blueCount; i++) {
                int blueBallIndex = (int) (Math.random() * rbbs);
                String blueBall = remainBlueBalls.get(blueBallIndex);
                System.out.println("fillVacancy:b:" + blueBall);
                selectBlueBalls.add(blueBall);
            }
        }

        List<String> redBallList=new ArrayList<String>();
        for(String redBall:selectRedBalls){
            redBallList.add(redBall);
        }
        List<String> blueBallList=new ArrayList<String>();
        for(String blueBall:selectBlueBalls){
            blueBallList.add(blueBall);
        }

        Unionlotto unionlotto=new Unionlotto(0,"","补缺一组",redBallList,blueBallList);
        result.add(unionlotto);

        return result;
    }

    //均匀算法
    public List<Unionlotto> averageMulti(int count,int redCount,int blueCount){
        if(redCount<6){
            redCount=6;
        }
        if(redCount>maxRedCount){
            redCount=maxRedCount;
        }
        if(blueCount<1){
            blueCount=1;
        }
        if(blueCount>maxBlueCount){
            blueCount=maxBlueCount;
        }

        //红球计算
        List<String> redBalls=new ArrayList<>();
        List<String> redBallsAll=new ArrayList<>();
        for(int i=1;i<=33;i++){
            redBallsAll.add(String.format("%02d",i));
        }
        Collections.shuffle(redBallsAll);
        int offset=0;
        for(int i=0;i<count;i++){
            if(offset+redCount>33){
                Collections.shuffle(redBallsAll);
                offset=0;
            }
            for(int n=0;n<redCount;n++){
                String s = redBallsAll.get(offset + n);
                redBalls.add(s);
            }
            offset+=redCount;
        }

        //蓝球计算
        List<String> blueBalls=new ArrayList<>();
        List<String> blueBallsAll=new ArrayList<>();
        for(int i=1;i<=16;i++){
            blueBallsAll.add(String.format("%02d",i));
        }
        Collections.shuffle(blueBallsAll);
        offset=0;
        for(int i=0;i<count;i++){
            if(offset+blueCount>16){
                Collections.shuffle(blueBallsAll);
                offset=0;
            }
            for(int n=0;n<blueCount;n++){
                String s = blueBallsAll.get(offset + n);
                blueBalls.add(s);
            }
            offset+=blueCount;
        }


        //组合为count组选号
        List<Unionlotto> result=new ArrayList<Unionlotto>();
        for(int i=0;i<count;i++){
            List<String> redBallList=new ArrayList<String>();
            for(int j=i*redCount;j<(i+1)*redCount;j++){
                redBallList.add(redBalls.get(j));
            }
            List<String> blueBallList=new ArrayList<String>();
            for(int j=i*blueCount;j<(i+1)*blueCount;j++){
                blueBallList.add(blueBalls.get(j));
            }
            Unionlotto unionlotto=new Unionlotto(0,"","均布一组",redBallList,blueBallList);
            result.add(unionlotto);
        }
        return result;
    }

    //追号两组
    public List<Unionlotto> trackMulti(int redCount, int blueCount){
        if(redCount<6){
            redCount=6;
        }
        if(redCount>maxRedCount){
            redCount=maxRedCount;
        }
        if(blueCount<1){
            blueCount=1;
        }
        if(blueCount>maxBlueCount){
            blueCount=maxBlueCount;
        }

        String[] redBallAll=new String[]{"02","03","05","06","07","08","09","10","11","12","13","14","17","20","21","24","26","27","28","29","31"};
        String[] blueBallAll=new String[]{"01","02","03","04","05","06","07","09","10","11","12","13","15","16"};

        int redSelectCount=(redCount-6)*2;
        List<Integer> redSelectIndex=new ArrayList<>();
        while (redSelectIndex.size() < redSelectCount && redSelectIndex.size() < redBallAll.length) {
            int index = (int) (Math.random() * redBallAll.length);
            if (!inList(redSelectIndex, index)) {
                redSelectIndex.add(index);
            }
        }

        int blueSelectCount=(blueCount-1)*2;
        List<Integer> blueSelectIndex=new ArrayList<>();
        while (blueSelectIndex.size() < blueSelectCount && blueSelectIndex.size() < blueBallAll.length) {
            int index = (int) (Math.random() * blueBallAll.length);
            if (!inList(blueSelectIndex, index)) {
                blueSelectIndex.add(index);
            }
        }


        List<Unionlotto> result=new ArrayList<Unionlotto>();//两组

        List<String> redBalls=new ArrayList<>();
        redBalls.addAll(Arrays.asList("01","04","15","18","22","32"));
        for(int i=0;i<redCount-6;i++){
            int idx=redSelectIndex.get(i);
            String b=redBallAll[idx];
            System.out.println("bbbbb->"+b+"<<<<");
            redBalls.add(b);
        }
        List<String> blueBalls= Collections.singletonList("14");
        for(int i=0;i<blueCount-1;i++){
            int idx=blueSelectIndex.get(i);
            blueBalls.add(blueBallAll[idx]);
        }
        result.add(new Unionlotto(0,"","追号一组", redBalls,blueBalls));

        redBalls=new ArrayList<>();
        redBalls.addAll(Arrays.asList("16","19","23","25","30","33"));
        for(int i=0;i<redCount-6;i++){
            int idx=redSelectIndex.get(i+redCount-6);
            redBalls.add(redBallAll[idx]);
        }
        blueBalls= Collections.singletonList("08");
        for(int i=0;i<blueCount-1;i++){
            int idx=blueSelectIndex.get(i+blueCount-1);
            blueBalls.add(blueBallAll[idx]);
        }
        result.add(new Unionlotto(0,"","追号二组", redBalls,blueBalls));

        return result;
    }
    private boolean inList(List<Integer> list,int i){
        for(int ii:list){
            if(ii==i){
                return true;
            }
        }
        return false;
    }

    public String getCurrentDate(){
        //计算日期
        Date now=new Date();
        Calendar cal = Calendar.getInstance();
        cal.setTime(now);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        int d=0;
        if(w>0&&w<=2){//星期2的
            d=2-w;
        }else if(w>2&&w<=4){//星期4的
            d=4-w;
        }else if(w>4){
            d=7-w;
        }
        cal.add(Calendar.DATE,d);
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String date=dateFormat.format(cal.getTime());
        return date;
    }

    public boolean appeared(Unionlotto unionlotto){
        String unionlottoStr=unionlotto.sprint();
        for(Unionlotto u:this.unionlottoList){
            String uStr=u.sprint();
            //System.out.println(uStr);
            if(unionlottoStr.equals(uStr)){
                System.out.println("equals ->"+unionlottoStr+","+uStr);
                return true;
            }
        }
        return false;
    }
    public boolean appeared(List<Unionlotto> unionlottos){
        for(Unionlotto u:unionlottos){
            if(appeared(u)){
                return true;
            }
        }
        return false;
    }
}
